package org.umlviewer.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import org.jdom2.Element;
import org.umlviewer.paint.PaintUtils;

public abstract class DiagramElement extends DrawElement {

    private int posX;
    private int posY;
    //determinar los limites del elemento
    private int height;
    private int width;
    //el rectangulo que encierra a la figura
    private Rectangle bounds;

    private Color fillColor=new Color(255, 255, 204);
    private Color lineColor=new Color(204,0,51);

    
    public int getPosX() {
        return posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public int getPosY() {
        return posY;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }
    
    public Color getFillColor() {
        return fillColor;
    }

    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

      public abstract void paint(Graphics g);
    
    /**
     * Devuelve el punto central del elemento
     * @return 
     */
    public Point getMidPoint()
    {
        return new Point(posX + width/2,posY + height/2);
    }    
    
    //pinta el rectangulo que encierra al elemento
    public void paintSelected(Graphics g)
    {
        g.setColor(Constants.SELECTIONCOLOR);
        g.drawRect(posX, posY, width, height);
        g.setColor(Constants.DEFAULTCOLOR);
        //pintar cuadrito superior
        g.fillRect(posX, posY, 5, 5);
        //superior derecho
        g.fillRect(posX+width-5, posY, 5, 5);
        //inf izq
        g.fillRect(posX, posY+height-5, 5, 5);
        //inf der
        g.fillRect(posX+width-5, posY+height-5, 5, 5);
    }
    /**
     * Retorna el rectangulo que limita la figura
     * @return 
     */
    @Override
    public Rectangle getBounds()
    {
        if(bounds == null)
            bounds = new Rectangle(posX, posY, width, height);
        else{
            bounds.x = posX;
            bounds.y = posY;
            bounds.width = width;
            bounds.height = height;
        }
        return bounds;
    }
    
    /**
     * Devuelve un punto para una relacion entre el elemento y
     * punto distante. Por defecto trabaja con rectangulos, sobrescribir
     * para otro tipo de figuras
     * @param away
     * @return 
     */
    public Point2D getRelPoint(Point away){
        return PaintUtils.ptClosestTo(getBounds(), away);
    }
    
  
    
    /*
     * Cambia el tamanio del elemento
     */
    @Override
    public void resize(Rectangle r){
        ;
    }
    
    //mover el elemento, equivale a setPosX, setPosY
    @Override
    public void move(int x, int y){
        this.posX = x;
        this.posY = y;
    }
    
    /***
     * Verifica si el punto x, y, hace parte del elemento
     * @param x
     * @param y
     * @return 
     */
    @Override
    public boolean contains(int x, int y){
          return ( (x >= posX && x<= posX + width )
                  && (y >= posY && y<= posY + height) );
    }
    
    /***
     * Serializar los elementos a XMI
     * @param parent nodo padre
     */
    public Element xmiSerialize(){
        return new Element("");
    }
}
